import java.io.*;
import java.util.*;
import java.net.URLDecoder;

public class HttpInputStream extends BufferedInputStream {
	private String fileName = null;
	private String method = null;
	private String version = null;
	private String resource = null;
	private Hashtable query = null;
	private Hashtable header = null;
	private Hashtable body = null;
	
	public HttpInputStream(InputStream in) {
		super(in);
		query = new Hashtable();
		header = new Hashtable();
		body = new Hashtable();
	}
	
	public void getRequest() throws IOException {
		StringTokenizer st = null;
		String tmp = null;
		st = new StringTokenizer(readLine());
		
		if(st.hasMoreTokens()) {
			method = st.nextToken();
		} else {
			throw new IOException();
		}
		
		if(st.hasMoreTokens()) {
			tmp = URLDecoder.decode(st.nextToken(), "UTF-8");
			if(tmp.indexOf('?') >= 0) {
				StringTokenizer tmpst = new StringTokenizer(tmp, "?");
				if(tmpst.hasMoreTokens()) {
					fileName = tmpst.nextToken();
					resource = HTTPconf.getResourcePath(fileName);
				}
				if(tmpst.hasMoreTokens()) {
					query = getQuery(tmpst.nextToken());
				}
			} else {
				fileName=tmp;
				resource = HTTPconf.getResourcePath(tmp);
			}
		} else {
			throw new IOException();
		}
		if(st.hasMoreTokens()) {
			version = st.nextToken();
		} else {
			throw new IOException();
		}
		if( version.equals(HTTP.HTTP_1_0) || version.equals(HTTP.HTTP_1_1)) {
			while(true) {
				String key = "";
				String value = "";
				tmp = readLine();
				if(tmp.equals("")) break;
				
				st = new StringTokenizer(tmp, ": ");
				if(st.hasMoreTokens()) { key=st.nextToken();}
				if(st.hasMoreTokens()) { value=st.nextToken();}
				
				header.put(key, value);
			}
		}
		if(method.equals(HTTP.METHOD_POST)) {
			int clen = Integer.parseInt((String)header.get("Content-Length"));
			StringBuffer sbuf = new StringBuffer();
			for(int i=0; i<clen; i++) {
				sbuf.append((char)super.read());
			}
			body = getQuery(URLDecoder.decode(sbuf.toString(), "UTF-8"));
		}
	}
	
	private String readLine() throws IOException {
		StringBuffer str = new StringBuffer();
		int ch = 0;
		while(true) {
			if( (ch==HTTP.CR)&((ch=super.read()) != -1) && (ch==HTTP.LF))
				break;
			str.append( (char)ch);
		}
		return str.toString().replaceAll("\r", "");
	}
	private Hashtable getQuery(String str) {
		Hashtable tmp = new Hashtable();
		StringTokenizer st = new StringTokenizer(str, "&");
		
		while(st.hasMoreTokens()) {
			StringTokenizer qst = 
				new StringTokenizer(st.nextToken(), "=");
			String key = null;
			String value = null;
			
			if(qst.hasMoreTokens()) { key=qst.nextToken();}
			if(qst.hasMoreTokens()) {
				value=qst.nextToken();
			} else{
				value="";
			}
			tmp.put(key, value);
		}
		return tmp;
	}
	
	public String getMethod() {
		return method;
	}
	public String getResourcePath() {
		return resource;
	}
	public String getVersion() {
		return version;
	}
	public Hashtable getQueryString() {
		return query;
	}
	public Hashtable getHeader() {
		return header;
	}
	public Hashtable getBody() {
		return body;
	}
	public String getFileName() {
		return fileName;
	}
}
